package com.atguigu.daijia.customer.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.customer.client.CustomerInfoFeignClient;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
@RequiredArgsConstructor
public class CustomerServiceImpl implements CustomerService {

    private final CustomerInfoFeignClient customerInfoFeignClient;
    private final RedisTemplate redisTemplate;

    /**
     * 微信小程序授权登录
     * @param code
     * @return
     */
    @Override
    public String login(String code) {
        // 1 远程调用 customer-service 接口，获取客户信息
        // Result<Long> result = customerInfoFeignClient.login(code);
        // if (result.getCode() != 200) {
        //     throw new GuiguException(ResultCodeEnum.FEIGN_FAIL);
        // }
        // Long customerId = result.getData();
        // if (customerId == null) {
        //     // 2 如果远程调用失败、获取不到客户信息，则抛出异常
        //     throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        // }
        Long customerId = customerInfoFeignClient.login(code).getData();
        // 3 生成 Token，返回给前端
        String token = UUID.randomUUID().toString().replace("-", "");
        // 4 将用户 id 存入 Redis，并设置过期时间。约定：key 是 token，value 是 customerId
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX + token, customerId.toString(), RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);

        return token;
    }

    /**
     * 获取登录用户的信息
     * @param token
     * @return
     */
    @Override
    public CustomerLoginVo getCustomerLoginInfo(String token) {
        String customerId = (String) redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);
        if (!StringUtils.hasText(customerId)) {
            // TODO 这里暂时抛出异常，实际上需要跳转到登录页面
            throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
        }
        Result<CustomerLoginVo> result = customerInfoFeignClient.getCustomerLoginInfo(Long.valueOf(customerId));
        if (result.getCode() != 200) {
            throw new GuiguException(ResultCodeEnum.FEIGN_FAIL);
        }
        CustomerLoginVo customerLoginVo = result.getData();
        if (customerLoginVo == null) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        return customerLoginVo;
    }

    /**
     * 获取登录用户的信息
     * @param customerId
     * @return
     */
    @Override
    public CustomerLoginVo getCustomerLoginInfo(Long customerId) {
        // Result<CustomerLoginVo> result = customerInfoFeignClient.getCustomerLoginInfo(customerId);
        // if (result.getCode() != 200) {
        //     throw new GuiguException(ResultCodeEnum.FEIGN_FAIL);
        // }
        // CustomerLoginVo customerLoginVo = result.getData();
        // if (customerLoginVo == null) {
        //     throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        // }
        // return customerLoginVo;
        return customerInfoFeignClient.getCustomerLoginInfo(customerId).getData();
    }

    /**
     * 绑定用户手机号
     * @param updateWxPhoneForm
     * @return
     */
    @Override
    public Boolean updateWxPhone(UpdateWxPhoneForm updateWxPhoneForm) {
        // Result<Boolean> result = customerInfoFeignClient.updateWxPhoneNumber(updateWxPhoneForm);
        // return result.getCode() == 200;
        return customerInfoFeignClient.updateWxPhoneNumber(updateWxPhoneForm).getData();
    }
}
